class_name StatsTracker
extends Resource

# 状态追踪器 - 使用组合模式管理多个追踪器
# 职责：协调各种追踪器的工作，提供统一接口

signal stats_tracked(tracker_name: String, event_type: String, data: Dictionary)

# 追踪器组件
var turn_counter: TurnCounter
var combat_tracker: CombatTracker
var resource_tracker: ResourceTracker

# 追踪器开关
@export var enable_turn_counter: bool = true
@export var enable_combat_tracker: bool = true
@export var enable_resource_tracker: bool = true

func _init():
	_initialize_trackers()

func _initialize_trackers() -> void:
	if enable_turn_counter:
		turn_counter = TurnCounter.new()
		turn_counter.counter_changed.connect(_on_counter_changed)
	
	if enable_combat_tracker:
		combat_tracker = CombatTracker.new()
		combat_tracker.combat_event.connect(_on_combat_event)
	
	if enable_resource_tracker:
		resource_tracker = ResourceTracker.new()
		resource_tracker.resource_changed.connect(_on_resource_changed)

# 事件处理器
func _on_counter_changed(counter_name: String, old_value: int, new_value: int) -> void:
	stats_tracked.emit("turn_counter", "changed", {
		"counter": counter_name,
		"old_value": old_value,
		"new_value": new_value
	})

func _on_combat_event(event_type: String, data: Dictionary) -> void:
	stats_tracked.emit("combat_tracker", event_type, data)

func _on_resource_changed(resource_type: String, old_value: int, new_value: int) -> void:
	stats_tracked.emit("resource_tracker", "changed", {
		"resource": resource_type,
		"old_value": old_value,
		"new_value": new_value
	})

# 统一接口 - 生命值相关
func track_health_change(old_health: int, new_health: int) -> void:
	var change = new_health - old_health
	
	if turn_counter:
		if change > 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.HEALTH_GAINED, change)
		elif change < 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.HEALTH_LOST, -change)
	
	if combat_tracker:
		combat_tracker.track_health_event(old_health, new_health)

# 统一接口 - 护盾相关
func track_block_change(old_block: int, new_block: int) -> void:
	var change = new_block - old_block
	
	if turn_counter:
		if change > 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.BLOCK_GAINED, change)
		elif change < 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.BLOCK_LOST, -change)
	
	if combat_tracker:
		combat_tracker.track_block_event(old_block, new_block)

# 统一接口 - 能量相关
func track_mana_change(old_mana: int, new_mana: int) -> void:
	var change = new_mana - old_mana
	
	if turn_counter:
		if change > 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.MANA_GAINED, change)
		elif change < 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.MANA_SPENT, -change)
	
	if resource_tracker:
		resource_tracker.track_mana_event(old_mana, new_mana)

# 统一接口 - 伤害相关
func track_damage_taken(damage: int, damage_to_health: int, damage_to_block: int) -> void:
	if turn_counter:
		if damage_to_health > 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.DAMAGE_TAKEN, damage_to_health)
		if damage_to_block > 0:
			turn_counter.add_to_counter(TurnCounter.CounterType.BLOCK_LOST, damage_to_block)
	
	if combat_tracker:
		combat_tracker.track_damage_event(damage, damage_to_health, damage_to_block)

# 统一接口 - 卡牌相关
func track_card_played() -> void:
	if turn_counter:
		turn_counter.add_to_counter(TurnCounter.CounterType.CARDS_PLAYED, 1)

func track_cards_drawn(amount: int) -> void:
	if turn_counter:
		turn_counter.add_to_counter(TurnCounter.CounterType.CARDS_DRAWN, amount)

func track_cards_discarded(amount: int) -> void:
	if turn_counter:
		turn_counter.add_to_counter(TurnCounter.CounterType.CARDS_DISCARDED, amount)

# 回合管理
func reset_turn_trackers() -> void:
	if turn_counter:
		turn_counter.reset_all_counters()
	if combat_tracker:
		combat_tracker.reset_turn_data()
	if resource_tracker:
		resource_tracker.reset_turn_data()

# 获取器接口
func get_turn_counter() -> TurnCounter:
	return turn_counter

func get_combat_tracker() -> CombatTracker:
	return combat_tracker

func get_resource_tracker() -> ResourceTracker:
	return resource_tracker

# 查询接口
func check_condition(tracker_type: String, condition_data: Dictionary) -> bool:
	match tracker_type:
		"turn_counter":
			if turn_counter:
				return turn_counter.check_condition(
					condition_data.get("counter_type", TurnCounter.CounterType.HEALTH_LOST),
					condition_data.get("condition_type", "at_least"),
					condition_data.get("threshold", 0)
				)
		"combat_tracker":
			if combat_tracker:
				return combat_tracker.check_combat_condition(condition_data)
		"resource_tracker":
			if resource_tracker:
				return resource_tracker.check_resource_condition(condition_data)
	return false

# 调试接口
func print_all_trackers() -> void:
	print("📊 === 所有追踪器状态 ===")
	if turn_counter:
		print("🔄 回合计数器:")
		turn_counter.print_all_counters()
	if combat_tracker:
		print("⚔️ 战斗追踪器:")
		combat_tracker.print_combat_stats()
	if resource_tracker:
		print("⚡ 资源追踪器:")
		resource_tracker.print_resource_stats()
	print("📊 ========================") 
